package com.tuishan.myapplication.Views.Users;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.tuishan.myapplication.Base.BaseContest;
import com.tuishan.myapplication.Custom.LoadingView;
import com.tuishan.myapplication.MainActivity;
import com.tuishan.myapplication.Model.RegisterModel;
import com.tuishan.myapplication.R;
import com.tuishan.myapplication.Servers.BaseCallback;
import com.tuishan.myapplication.Servers.OkHttpHelper;
import com.tuishan.myapplication.Servers.UrlRouter;

import java.util.WeakHashMap;

import okhttp3.Request;
import okhttp3.Response;

public class SetUserInfoActivity extends Activity implements OnTitleBarListener, View.OnClickListener {
    LoadingView Loading;
    TitleBar titleBar;
    Context mContext;
    CheckBox Set_Click_Boy, Set_Click_Girl;
    Button Set_UserInfo_btn;
    EditText Set_Re_nickname_edit, Set_School_edit, Set_Phone_edit, Set_Address_edit, Set_Birthday_edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_user_info);
        mContext = this;
        initView();
    }

    private void initView() {
        titleBar = findViewById(R.id.titleBar);
        Set_UserInfo_btn = findViewById(R.id.Set_UserInfo_btn);
        Set_Click_Boy = findViewById(R.id.Set_Click_Boy);
        Set_Click_Girl = findViewById(R.id.Set_Click_Girl);

        Set_Re_nickname_edit = findViewById(R.id.Set_Re_nickname_edit);
        Set_School_edit = findViewById(R.id.Set_School_edit);
        Set_Phone_edit = findViewById(R.id.Set_Phone_edit);
        Set_Address_edit = findViewById(R.id.Set_Address_edit);
        Set_Birthday_edit = findViewById(R.id.Set_Birthday_edit);

        titleBar.setOnTitleBarListener(this);
        Set_UserInfo_btn.setOnClickListener(this);
        Set_Click_Boy.setOnClickListener(this);
        Set_Click_Girl.setOnClickListener(this);
        Loading = new LoadingView(mContext);
    }


    /**
     * 用户信息更新
     */
    private void UpDateBtn(String nickname, String school, String phone, String address, String birthday) {

        if (StringUtils.isEmpty(nickname)) {
            Toast.makeText(this, "请确认昵称输入是否正确", Toast.LENGTH_SHORT).show();
            return;
        }
        if (StringUtils.isEmpty(phone)) {
            Toast.makeText(this, "请确认手机号输入是否正确", Toast.LENGTH_SHORT).show();
            return;
        }
        if (StringUtils.isEmpty(address)) {
            Toast.makeText(this, "请确认地址输入是否正确", Toast.LENGTH_SHORT).show();
            return;
        }
        if (StringUtils.isEmpty(birthday)) {
            Toast.makeText(this, "请确认生日输入是否正确", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!(Set_Click_Boy.isChecked() || Set_Click_Girl.isChecked())) {
            Toast.makeText(this, "请选择性别", Toast.LENGTH_SHORT).show();
            return;
        }
        Loading.show();
        WeakHashMap<String, Object> objectMap = new WeakHashMap<>();
        objectMap.put("areaCode", address);
        objectMap.put("phone", phone);
        objectMap.put("nickName", nickname);
        objectMap.put("birthday", Integer.parseInt(birthday));
        objectMap.put("school", school);
        objectMap.put("sex", Set_Click_Boy.isChecked() ? 1 : 2);
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.post(UrlRouter.UpDateUrl, String.class, objectMap, new BaseCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                Loading.dismiss();
            }

            @Override
            public void onSuccess(Response response, Object o) {
                Loading.dismiss();
                Toast.makeText(mContext, "修改成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFalse(Response response, String err_msg) {
                Loading.dismiss();
                Toast.makeText(mContext, err_msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Loading.dismiss();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.Set_UserInfo_btn:
                UpDateBtn(Set_Re_nickname_edit.getText().toString().trim(),Set_School_edit.getText().toString().trim(),
                        Set_Phone_edit.getText().toString().trim(),Set_Address_edit.getText().toString().trim(),
                        Set_Birthday_edit.getText().toString().trim());
                break;
            case R.id.Set_Click_Girl:
                if (Set_Click_Girl.isChecked()) {
                    Set_Click_Boy.setChecked(false);
                }
                break;
            case R.id.Set_Click_Boy:
                if (Set_Click_Boy.isChecked()) {
                    Set_Click_Girl.setChecked(false);
                }
                break;

        }
    }

    @Override
    public void onLeftClick(View v) {
        finish();
    }

    @Override
    public void onTitleClick(View v) {

    }

    @Override
    public void onRightClick(View v) {

    }
}
